package com.salt.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * 全局异常处理器
 */
@Slf4j
@ControllerAdvice
public class ExceptionAdvice  {


    @ExceptionHandler(value = BusinessException.class)
    @ResponseBody
    public ReturnMessageBean<?> handle(BusinessException e) {
        log.error(e.getMessage(), e);
        return ReturnMessageBean.errorMessageBean(e.getMessage(),String.valueOf(e.getCode()));
    }

    @ExceptionHandler(value = ApiException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ReturnMessageBean<?> handle(ApiException e) {
        log.error(e.getMessage(), e);
        return ReturnMessageBean.errorMessageBean(e.getMessage());
    }

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ReturnMessageBean<?> handle(Exception e) {
        log.error(e.getMessage(), e);
        return ReturnMessageBean.errorMessageBean(e.getMessage());
    }

}
